﻿using ExcelConfig;
using filecopy.io.Container;
using filecopy.io;

namespace filecopy
{
    internal class Program
    {
        static int Main(string[] args)
        {
            try
            {
                var printerr = AppConfig.YesOrNo("当无法执行后输出错误信息");
                var printinfo = AppConfig.YesOrNo("当完成后输出执行信息");
                var filterdate = AppConfig.YesOrNo("按时间选取源");

                var dates = new List<string>(args.Length + 1);
                if (filterdate)
                    foreach (var arg in args)
                    {
                        if (DateTime.TryParse(arg, out var date)) dates.Add(date.ToString("yyyy_MM_dd"));
                    }

                if (dates.Count == 0)
                {
                    if (AppConfig.YesOrNo("默认选取当天"))
                    {
                        dates.Add(DateTime.Now.ToString("yyyy_MM_dd"));
                    }
                }

                if (dates.Count == 0)
                {
                    if (printerr) Console.WriteLine("请选择日期，当前为获取到，即将退出执行。");
                    return -1;
                }

                var sourcePath = AppConfig.Text("源目录");
                var needBackupTags = AppConfig.Array("源选取标识");
                var distPath = AppConfig.Text("目标目录");
                var distAuth_user = AppConfig.Text("目标认证用户名");
                var distAuth_passwd = AppConfig.Text("目标认证密码");
                var noRepeat = AppConfig.YesOrNo("防止重复模式");
                var categoryWay = AppConfig.Text("目标分类细度");

                var fs_container = new FsContainer();
                fs_container.To(sourcePath);
                fs_container.SetFilterTags(needBackupTags);

                using var smbc = new SmbContainer();
                {
                    var paths = new Queue<string>(distPath.Split("/").Where(t => !string.IsNullOrWhiteSpace(t)));
                    smbc.Login(paths.Dequeue(), distAuth_user, distAuth_passwd);
                    smbc.To(string.Join("/", paths));
                }

                foreach (var date in dates)
                {
                    if (printinfo) Console.WriteLine($"当前执行选取标识: {date}");
                    var items = filterdate ? fs_container.FindItems(date) : fs_container.FindItems().ToList();

                    DateTime.TryParse(date.Replace("_", "-"), out var _date);

                    smbc.To("./" + _date.ToString("yyyy"));
                    smbc.To("./" + _date.ToString("yyyyMM"));
                    smbc.To("./" + _date.ToString("yyyyMMdd"));

                    var smb_path = smbc.TakeNowPath();
                    foreach (var item in items)
                    {
                        if (noRepeat)
                        {
                            if (smbc.Ls().Count(t => t.Contains(item.FileName, StringComparison.OrdinalIgnoreCase)) != 0) continue;
                        }

                        if (printinfo) Console.WriteLine($"{item.FileName} -> {smb_path.Path}");
                        if (printinfo) Console.WriteLine($"现有数据：{item.ReadSize()} byte。");

                        var copysize = CopyCenter.Copy(item, smb_path);
                        if (printinfo)
                        {
                            if (copysize == item.ReadSize()) Console.WriteLine($"成功复制：{copysize} byte。");
                            else Console.WriteLine($"* 异常复制：{copysize} byte。");
                        }
                        if (printinfo) Console.WriteLine($"");
                    }
                    smbc.To("../../../");
                }

                return 0;
            }catch (Exception e)
            {
                Console.WriteLine(e.Message); return -1;
            }
        }
    }
}
